package com.oocl.websocket;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;

import com.oocl.service.MerchantsService;


@Controller(value = "webSocketHandler")
@RequestMapping("/ws/push")
public class WebSocketHandler extends TextWebSocketHandler {

	private static Map<String, List<WebSocketSession>> sensMap;
	private List<WebSocketSession> sens;

	public WebSocketHandler() {
		super();
		sensMap=new HashMap<String, List<WebSocketSession>>();
		sens = new ArrayList<WebSocketSession>();
	}

	@Autowired
	private MerchantsService merchantsService;
	
	public void handleTextMessage(WebSocketSession session, TextMessage message) {
		System.out.println("text message: " + session.getId() + "-"
				+ message.getPayload());
		String msg=message.getPayload();
		System.out.println(msg.length());
		if (msg.length()==18) {           //保存商家session,根据商家icnum保存list<session>
			if (sensMap.get(msg)!=null) {
				sensMap.get(msg).add(session);
				System.out.println(sensMap.get(msg).size()+"************************");
			}else {
				List<WebSocketSession> sList=new ArrayList<WebSocketSession>();
				sList.add(session);
				sensMap.put(msg, sList);
			}
		}else if(msg.length()>18){
			String icnum=msg.substring(0, 18);
			String status=msg.substring(19);
			TextMessage m=new TextMessage(status);
			for (WebSocketSession sen :sensMap.get(icnum)) {
				try {					
					sen.sendMessage(m);
//					System.out.println("m:"+m);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	@Override
	public void afterConnectionEstablished(WebSocketSession session)
			throws Exception {
		// 与客户端完成连接后调用
		System.out.println("afterConnectionEstablished");
		System.out.println("getId:" + session.getId());
		System.out.println("getLocalAddress:"
				+ session.getLocalAddress().toString());
		System.out.println("getTextMessageSizeLimit:"
				+ session.getTextMessageSizeLimit());
		System.out.println("getUri:" + session.getUri().toString());
		System.out.println("getPrincipal:" + session.getPrincipal());
		System.out.println(session.getRemoteAddress());
	}

	@Override
	public void handleTransportError(WebSocketSession session,
			Throwable exception) throws Exception {
		// 消息传输出错时调用
		System.out.println("-=====================-");
		System.out.println("handleTransportError");
		System.out.println("-=====================-");
	}

	@Override
	public void afterConnectionClosed(WebSocketSession session,
			CloseStatus closeStatus) throws Exception {
		// 一个客户端连接断开时关闭
		System.out.println("-=====================-");
		System.out.println("afterConnectionClosed");
		System.out.println(session.getRemoteAddress());
		System.out.println("-=====================-");
		for (String s:sensMap.keySet()) {
			sensMap.get(s).remove(session);
		}
	}

	@Override
	public boolean supportsPartialMessages() {
		return false;
	}

}
